package com.drawingtimer.admin.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.drawingtimer.admin.converter.TProcessInfoConverter;
import com.drawingtimer.admin.enums.TProcessInfoEnum;
import com.drawingtimer.admin.model.entity.TProcessInfo;
import com.drawingtimer.admin.model.qo.TProcessInfoQO;
import com.drawingtimer.admin.model.vo.TProcessInfoPageVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hccake.ballcat.common.model.domain.PageParam;
import com.hccake.ballcat.common.model.domain.PageResult;
import com.hccake.extend.mybatis.plus.conditions.query.LambdaQueryWrapperX;
import com.hccake.extend.mybatis.plus.mapper.ExtendMapper;
import com.hccake.extend.mybatis.plus.toolkit.WrappersX;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 工序信息
 *
 * @author jjh 2025-04-24 16:34:17
 */
public interface TProcessInfoMapper extends ExtendMapper<TProcessInfo> {

	/**
	 * 分页查询
	 * @param pageParam 分页参数
	 * @param qo 查询参数
	 * @return PageResult<TProcessInfoPageVO> VO分页数据
	 */
	default PageResult<TProcessInfoPageVO> queryPage(PageParam pageParam, TProcessInfoQO qo) {

		if (qo != null && StringUtils.isNotBlank(qo.getFlowCardNumber())) {
			qo.setFlowCardNumber(qo.getFlowCardNumber().trim());
		}

		// 启动分页
		PageInfo<TProcessInfoPageVO> objectPageInfo = PageHelper
			.startPage((int) pageParam.getPage(), (int) pageParam.getSize())
			.doSelectPageInfo(() -> this.page(qo));

		objectPageInfo.getList().forEach(vo -> {
			TProcessInfoEnum status = TProcessInfoEnum.getByCode(vo.getStatus());
			if (status != null) {
				vo.setStatusMsg(status.getDesc());
			}
			if (vo.getSetupTime() != null && vo.getSetupTime().compareTo(BigDecimal.ZERO) > 0) {
				// 转换 1 为一小时 1.5 为 1.5小时 转换为 分钟
				vo.setSetupTime(vo.getSetupTime().multiply(new BigDecimal(60)));
			}
			if (vo.getStandardTime() != null && vo.getStandardTime().compareTo(BigDecimal.ZERO) > 0) {
				// 转换 1 为一小时 1.5 为 1.5小时 转换为 分钟
				vo.setStandardTime(vo.getStandardTime().multiply(new BigDecimal(60)));
			}
		});
		return new PageResult<>(objectPageInfo.getList(), objectPageInfo.getTotal());
	}

	default PageResult<TProcessInfoPageVO> getDetailsByFlowCardNumber(String flowCardNumber) {

		List<TProcessInfoPageVO> page = getReportingForWorkByFlowCardNumber(flowCardNumber);

		for (TProcessInfoPageVO vo : page) {
			TProcessInfoEnum status = TProcessInfoEnum.getByCode(vo.getStatus());
			if (status != null) {
				vo.setStatusMsg(status.getDesc());
			}
			if (vo.getSetupTime() != null && vo.getSetupTime().compareTo(BigDecimal.ZERO) > 0) {
				// 转换 1 为一小时 1.5 为 1.5小时 转换为 分钟
				vo.setSetupTime(vo.getSetupTime().multiply(new BigDecimal(60)));
			}
			if (vo.getStandardTime() != null && vo.getStandardTime().compareTo(BigDecimal.ZERO) > 0) {
				// 转换 1 为一小时 1.5 为 1.5小时 转换为 分钟
				vo.setStandardTime(vo.getStandardTime().multiply(new BigDecimal(60)));
			}
		}
		return new PageResult<>(page, page.size());

	}

	List<TProcessInfoPageVO> getReportingForWorkByFlowCardNumber(@Param("flowCardNumber") String flowCardNumber);

	List<TProcessInfoPageVO> page(@Param("qo") TProcessInfoQO qo);

}